/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kosongkosong.controller;

import com.kosongkosong.model.Match;
import com.kosongkosong.model.Phase;
import com.kosongkosong.service.DataService;
import com.kosongkosong.wrapper.MatchListWrapper;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author ifnu
 */
@Controller
public class ScheduleController {

    @Autowired private DataService dataService;

    @RequestMapping(value="/group-stage/{gmt}",method=RequestMethod.GET)
    public ModelAndView getGroupStageSchedule(@PathVariable("gmt") int gmt){
        ModelAndView view = new ModelAndView();
        List<Match> matches = dataService.getSchedule(Phase.gp);
        for (Match match : matches) {
            match.setGmt(gmt);
        }
        MatchListWrapper wrapper = new MatchListWrapper();
        wrapper.setMatches(matches);
        view.addObject(wrapper);
        return view;
    }
    
    @RequestMapping(value="/schedule/phase/{phase}/{gmt}",method=RequestMethod.GET)
    public ModelAndView getSchedule(@PathVariable("phase") String phase,
            @PathVariable("gmt") int gmt){
        ModelAndView view = new ModelAndView();
        Phase selectedPhase = null;
        if(phase!=null){
            for(Phase p :Phase.values()){
                if(p.name().equals(phase)){
                    selectedPhase = p;
                    break;
                }
            }
        } else {
            selectedPhase = Phase.gp;
        }

        List<Match> matches = dataService.getSchedule(selectedPhase);
        for (Match match : matches) {
            match.setGmt(gmt);
        }
        MatchListWrapper wrapper = new MatchListWrapper();
        wrapper.setMatches(matches);
        view.addObject(wrapper);
        return view;
    }
    
    @RequestMapping(value="/group-stage/{group-name}/{gmt}",method=RequestMethod.GET)
    public ModelAndView getGroupStageSchedule(@PathVariable("group-name") String groupName,
            @PathVariable("gmt") int gmt){
        ModelAndView view = new ModelAndView();
        List<Match> matches = new ArrayList<Match>();
        if(groupName != null){
            matches = dataService.getGroupPhaseSchedule(groupName);
        }
        for (Match match : matches) {
            match.setGmt(gmt);
        }
        MatchListWrapper wrapper = new MatchListWrapper();
        wrapper.setMatches(matches);
        view.addObject(wrapper);
        return view;
    }

}
